/**
 * 神州信息(2012)
 */
package net.abbp.report.model.io;

import java.util.HashMap;
import java.util.Map;

import net.abbp.report.model.ReportModel;
import net.abbp.report.model.headerfooter.GroupPageNumHFElement;
import net.abbp.report.model.headerfooter.GroupTotalPagesHFElement;
import net.abbp.report.model.headerfooter.NewLineHFElement;
import net.abbp.report.model.headerfooter.PageNumHFElement;
import net.abbp.report.model.headerfooter.TextHFElement;
import net.abbp.report.model.headerfooter.TotalPagesHFElement;
import net.abbp.report.model.headerfooter.io.GroupPageNumHFElementIO;
import net.abbp.report.model.headerfooter.io.GroupTotalPagesHFElementIO;
import net.abbp.report.model.headerfooter.io.NewLineHFElementIO;
import net.abbp.report.model.headerfooter.io.PageNumHFElementIO;
import net.abbp.report.model.headerfooter.io.TextHFElementIO;
import net.abbp.report.model.headerfooter.io.TotalPagesHFElementIO;
import net.abbp.report.model.io.impl.DefaultValueIO;
import net.abbp.report.view.cell.value.FormulaValue;
import net.abbp.report.view.cell.value.ImageValue;
import net.abbp.report.view.cell.value.JsDataValue;

/**
 * 值IO管理器
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        <pre>
 * 历史：
 *      建立: Jul 28, 2012 lexloo
 * </pre>
 */
public enum ValueIOManager {
    INSTANCE;

    private Class<?> defaultClazz = Object.class;
    /**
     * IValueIO容器，类作为键值
     */
    private Map<Class<?>, IValueIO> valueIOMap = new HashMap<Class<?>, IValueIO>();
    /**
     * IValueIO容器,类名作为键值
     */
    private Map<String, IValueIO> valueIOMap1 = new HashMap<String, IValueIO>();

    ValueIOManager() {
        register(defaultClazz, new DefaultValueIO());
        register(FormulaValue.class, new FormulaValue.IO());
        register(ImageValue.class, new ImageValue.IO());
        register(JsDataValue.class, new JsDataValue.IO());
        register(ReportModel.class, new ReportModel.IO());
        register(TextHFElement.class, new TextHFElementIO());
        register(NewLineHFElement.class, new NewLineHFElementIO());
        register(GroupPageNumHFElement.class, new GroupPageNumHFElementIO());
        register(GroupTotalPagesHFElement.class, new GroupTotalPagesHFElementIO());
        register(PageNumHFElement.class, new PageNumHFElementIO());
        register(TotalPagesHFElement.class, new TotalPagesHFElementIO());
    }

    /**
     * 对象注册
     * 
     * @param clazz 注册类
     * @param io 注册的对象
     */
    public void register(Class<?> clazz, IValueIO io) {
        valueIOMap.put(clazz, io);
        valueIOMap1.put(clazz.getName(), io);
    }

    /**
     * 通过类名获取IValueIO
     * 
     * @param clazz 类名
     * @return IValueIO 对象
     */
    public IValueIO getValueIO(Class<?> clazz) {
        IValueIO io = valueIOMap.get(clazz);

        if (io == null) {
            io = valueIOMap.get(defaultClazz);
        }

        return io;
    }

    /**
     * 通过类名字符串获取IValueIO
     * 
     * @param clazzName 类名
     * @return IValueIO 对象
     */
    public IValueIO getValueIO(String clazzName) {
        IValueIO io = valueIOMap1.get(clazzName);

        if (io == null) {
            io = valueIOMap.get(defaultClazz);
        }

        return io;
    }
}
